package com.lahuan.config.aop.mycache;

import com.lahuan.config.aop.mycache.cache.Cache;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sun.nio.cs.SingleByte;

import javax.annotation.Resource;

@Aspect
@Component
public class AopMyCache {
    /**
     * 自定义的cache实现
     */
    @Resource
    Cache cache;


    //环绕增强
    @Around("@annotation(myCache)")
    public Object around(ProceedingJoinPoint proceedingJoinPoint,MyCache myCache) throws Throwable {
        try {
            //
            Signature signature = proceedingJoinPoint.getSignature();
            String methodKey = signature.toString();
            Object[] args = proceedingJoinPoint.getArgs();
            Object o = cache.get(methodKey, args);
            if(o !=null)
            {
                System.out.println("AopMyCache 命中缓存:"+methodKey);
                return o;
            }
            Object result = proceedingJoinPoint.proceed();
            cache.put(methodKey,args,result,myCache.value());
            return result;
        } catch (Throwable throwable) {
            throw throwable;

        }

    }
}
